﻿using System.Collections.Generic;
using KC.Framework.Tenant;
using KC.Model.Storage.Table;
using KC.Service.Storage.Core;
using KC.Storage.Core.Repository.File;

namespace KC.Service.Storage
{
    public class FileService : StorageServiceBase, IThreadService
    {
        protected DefaultThreadConfigRepository ThreadConfigInfoRepository { get; set; }
        protected DefaultThreadStatusRepository ThreadStatusInfoRepository { get; set; }

        public FileService()
        {
            ThreadConfigInfoRepository = new DefaultThreadConfigRepository();
            ThreadStatusInfoRepository = new DefaultThreadStatusRepository();
        }

        #region Thread

        public ThreadConfigInfo GetWorkerRoleConfigToRun(string workerRoleId, string hostName)
        {
            return ThreadConfigInfoRepository.GetWorkerRoleConfigToRun(workerRoleId, hostName);
        }

        public void DeleteWorkerRoleConfigInfo(string rowKey)
        {
            ThreadConfigInfoRepository.DeleteWorkerRoleConfigInfo(rowKey);
        }

        public void DeleteUselessWorkerRoleConfig()
        {
            ThreadConfigInfoRepository.DeleteUselessWorkerRoleConfig();
        }

        public void DeleteAllWorkerRoleConfig()
        {
            ThreadConfigInfoRepository.DeleteAllWorkerRoleConfig();
        }

        public List<ThreadStatusInfo> GetThreadStatus()
        {
            return ThreadStatusInfoRepository.GetThreadStatus();
        }

        public void InitializeWorkerRoleThreadStatus(string workerRoleId, string threadname)
        {
            ThreadStatusInfoRepository.InitializeWorkerRoleThreadStatus(workerRoleId, threadname);
        }

        public void UpdateWorkerRoleDate(string workerRoleId)
        {
            ThreadStatusInfoRepository.UpdateWorkerRoleDate(workerRoleId);
        }

        public void UpdateWorkerRoleThreadStatus(string workerRoleId, string threadname, int successCount, int failCount)
        {
            ThreadStatusInfoRepository.UpdateWorkerRoleThreadStatus(workerRoleId, threadname, successCount, failCount);
        }

        public void ResetAllWorkerRoleThreadStatus()
        {
            ThreadStatusInfoRepository.ResetAllWorkerRoleThreadStatus();
        }

        #endregion
    }

    public class TenantFileService : FileService
    {
        public TenantFileService(Tenant tenant)
        {
            base.Tenant = tenant;

            ThreadConfigInfoRepository = new DefaultThreadConfigRepository(tenant);
            ThreadStatusInfoRepository = new DefaultThreadStatusRepository(tenant);
        }
    }
}
